package com.cloud.school.bus.teacherhelper.protocol.notice;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Environment;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.utils.FileUtils;
import com.cloud.school.bus.teacherhelper.entitys.Notice;
import com.cloud.school.bus.teacherhelper.entitys.Notice.PList;
import com.cloud.school.bus.teacherhelper.protocol.baseresponse.BaseJsonHttpResponse;

public class GetNoticeListResponse extends BaseJsonHttpResponse {

	protected List<Notice> noticeList = new ArrayList<Notice>();
	
	@Override
	public void onSuccess(int statusCode, Header[] headers,
			String responseString) throws Throwable {
		if ("1".equals(code)) {
			// DebugLog.logI(responseString);
//			FileUtils.saveFile(responseString, new File(Environment
//					.getExternalStorageDirectory().getAbsolutePath()
//					+ "/notice.json"));
			JSONArray mainJsonArray = new JSONArray(responseString);
			for (int i = 0; i < mainJsonArray.length(); i++) {
				JSONObject itemJsonObject = mainJsonArray.optJSONObject(i);
				if(null != itemJsonObject){
					Notice notice = new Notice();
					notice.noticekey = itemJsonObject.optString("noticekey","");
					notice.noticetitle = itemJsonObject.optString("noticetitle","");
					notice.noticecontent = itemJsonObject.optString("noticecontent","");
					notice.noticeid = itemJsonObject.optString("noticeid","");
					notice.adduserid = itemJsonObject.optString("adduserid","");
					notice.addtime = itemJsonObject.optString("addtime","");
					notice.isconfirm = itemJsonObject.optString("isconfirm","");
					notice.teachername = itemJsonObject.optString("teachername","");
					
					JSONArray slistJsonArray = itemJsonObject.optJSONArray("slist");
					if(null != slistJsonArray){
						String[] slistStrings = new String[slistJsonArray.length()];
						for (int j = 0; j < slistJsonArray.length(); j++) {
							slistStrings[j] = slistJsonArray.optString(j, "");
						}
						notice.slist = slistStrings;
					}
					
					JSONArray slistnameJsonArray = itemJsonObject.optJSONArray("slistname");
					if(null != slistnameJsonArray){
						String[] slistnameJsonArrayStrings = new String[slistnameJsonArray.length()];
						String[] slistnameFlagJsonArrayStrings = new String[slistnameJsonArray.length()];
						for (int j = 0; j < slistnameJsonArray.length(); j++) {
							slistnameJsonArrayStrings[j] = slistnameJsonArray.optString(j, "");
							slistnameFlagJsonArrayStrings[j] = "0";
						}
						notice.slistname = slistnameJsonArrayStrings;
						notice.sisconfirmFlag = slistnameFlagJsonArrayStrings;
					}
					
					JSONArray sisconfirmJsonArray = itemJsonObject.optJSONArray("sisconfirm");
					if(null != sisconfirmJsonArray){
						String[] sisconfirmStrings = new String[sisconfirmJsonArray.length()];
						for (int j = 0; j < sisconfirmJsonArray.length(); j++) {
							sisconfirmStrings[j] = sisconfirmJsonArray.optString(j, "");
						}
						notice.sisconfirm = sisconfirmStrings;
						
						for (int j = 0; j < notice.slistname.length; j++) {
							for (int j2 = 0; j2 < notice.sisconfirm.length; j2++) {
								if(notice.slistname[j].equals(notice.sisconfirm[j2])){
									notice.sisconfirmFlag[j] = "1";
									break;
								}
							}
						}
					}
					
					JSONArray plistJsonArray = itemJsonObject.optJSONArray("plist");
					if(null != plistJsonArray){
						List<PList> pArrayList = new ArrayList<Notice.PList>();
						for (int j = 0; j < plistJsonArray.length(); j++) {
							JSONObject plistItemJsonObject = plistJsonArray.optJSONObject(j);
							if(null != plistItemJsonObject){
								PList pList = new PList();
								pList.source = plistItemJsonObject.optString("source","");
								pList.filename = plistItemJsonObject.optString("filename","");
								pList.iscloud = plistItemJsonObject.optString("iscloud","");
								pArrayList.add(pList);
							}
						}
						notice.plist = pArrayList;
					}
					noticeList.add(notice);
				}
			}
		}
	}

}
